<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>保单列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<form class="layui-form">

			<div class="layui-inline dynamicShow1">
				<label class="layui-form-label">门店</label>
				<div class="layui-input-block">
					<select name="merchantId" id="merchantId">
						<option value="">--请选择门店--</option>
					</select>
				</div>
			</div>

        <div class="layui-inline">
            <label class="layui-form-label">保单号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="code" placeholder="请输入保单号">
            </div>
        </div>
         <div class="layui-inline">
			<label class="layui-form-label">手机号码</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="phone" placeholder="请输入手机号码">
			</div>
		</div>
			<div class="layui-inline" >
				<label class="layui-form-label">保单状态</label>
				<div class="layui-input-block">
					<select name="status" id="status">
						<option value="">--请选择状态--</option>
						<option value="-1">已过期</option>
						<option value="1">即将过期</option>
						<option value="0">正常</option>

					</select>
				</div>
			</div>

			<div class="layui-inline">
				<label class="layui-form-label">范围</label>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="minPolicyMoney" placeholder="￥" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid">-</div>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="maxPolicyMoney" placeholder="￥" autocomplete="off" class="layui-input">
				</div>
			</div>
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit="" lay-filter="queryPolicy">查询</a>
        </div>
        <div class="layui-inline dynamicShow">
           <a class="layui-btn layui-btn-normal policysAdd_btn">添加保单</a>
        </div>
    </form>
		
	</blockquote>
	<div class="layui-form news_list">
	  	<table class="layui-table" id="policyList" lay-filter="policys"></table>
	</div>
	<div id="page"></div>
	<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
	<script type="text/javascript" src="js/user/newsList.js"></script>
	
		    <script type="text/html" id="barDemo">
		  <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>编辑</a>
	    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>

	  </script>
	<script type="text/javascript">
	//重新渲染表单
	function renderForm(){
	  layui.use('form', function(){
	   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	   form.render();
	  });
	 }
	layui.config({
	    base: '/js/admin/'
	}).extend({
	    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
	    $tool: 'tool'
	}).use(['table','form','$tool'], function(){
	    var table = layui.table;
	    var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	    $tool = layui.$tool;
	    //方法级渲染
        $.ajax({
            type : "GET",
            url: '/merchants/merchants.json',
            dataType:'json',
            async:false,
            success: function(data){
                var list=data.data;
                var html='';
                for(var i=0;i<list.length;i++){
                    html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>"
                }
                $("#merchantId").append(html);
                form.render();
            }
        });

        var isAdminstr=localStorage.getItem("isAdmin");
        var isAdmin=isAdminstr=='true'?true:false;
        var tableIns;
        if(isAdmin){
            $(".dynamicShow").hide();
            $(".dynamicShow1").show();
            tableIns= table.render({
                elem: '#policyList'
                ,url: '/policys'
                ,cols: [[
                    {checkbox: true, fixed: true}
                    ,{field:'belongMerchantName',title:"所属门店"}
                    ,{field:'companyType', title: '保险公司',  width:'8%', templet:
                        function(d){
                            if(d.companyType==1){
                                return "中国人保";
                            }else if(d.companyType==2){
                                return "中国人寿";
                            }else if(d.companyType==3){
                                return "中国平安";
                            }else if(d.companyType==4){
                                return "太平洋保险";
                            }else if(d.companyType==5){
                                return "中国太平";
                            }else if(d.companyType==6){
                                return "友邦保险";
                            }else if(d.companyType==7){
                                return "新华保险";
                            }else if(d.companyType==8){
                                return "泰康保险";
                            }else if(d.companyType==9){
                                return "阳光保险";
                            }else if(d.companyType==10){
                                return "大地车险";
                            }else if(d.companyType==11){
                                return "天安车险";
                            }else if(d.companyType==12){
                                return "安邦车险";
                            }else if(d.companyType==13){
                                return "华泰车险";
                            }else if(d.companyType==14){
                                return "安盛天平车险";
                            }else if(d.companyType==15){
                                return "中银车险";
                            }else if(d.companyType==16){
                                return "永诚车险";
                            }
                        }
                    }
                    ,{field:'code', title: '保单号',  width:'8%'}
                    ,{field:'applicant', title: '投保人',  width:'6%'}
                    ,{field:'phone', title: '手机号码',  width:'8%'}
                    ,{field:'plateNumber', title: '车牌号',  width:'8%'}
                    ,{field:'frameNumber', title: '车架号',  width:'8%'}
                    ,{field:'carInfo', title: '汽车品牌',  width:'10%'}
                    ,{field:'policyMoney', title: '投保金额',  width:'6%'}
                    ,{field:'returnMoney', title: '礼包金额',  width:'6%'}
                    ,{field:'expiryTime', title: '参保日期',  width:'6%',templet:
                        function(d){
                            return $tool.formatDate(d.expiryTime);
                        }
                    }
                    , {field: 'status', title: '保单状态', width: '7%', templet:
                        function (d) {
                            if (d.status == 1) {
                                return "正常"
                            } else if (d.status == 0) {
                                return "即将过期"
                            } else if (d.status == -1) {
                                return "已过期"
                            }
                        }

                    }
                    ,{title: '操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器


                ]]
                ,id: 'testReload'
                ,page: true
//	        ,height: 600
            });

        }else {
            $(".dynamicShow").show();
            $(".dynamicShow1").hide();

            tableIns= table.render({
                elem: '#policyList'
                ,url: '/policys'
                ,cols: [[
                    {checkbox: true, fixed: true}
                    ,{field:'companyType', title: '保险公司',  width:'8%', templet:
                        function(d){
                            if(d.companyType==1){
                                return "中国人保";
                            }else if(d.companyType==2){
                                return "中国人寿";
                            }else if(d.companyType==3){
                                return "中国平安";
                            }else if(d.companyType==4){
                                return "太平洋保险";
                            }else if(d.companyType==5){
                                return "中国太平";
                            }else if(d.companyType==6){
                                return "友邦保险";
                            }else if(d.companyType==7){
                                return "新华保险";
                            }else if(d.companyType==8){
                                return "泰康保险";
                            }else if(d.companyType==9){
                                return "阳光保险";
                            }else if(d.companyType==10){
                                return "大地车险";
                            }else if(d.companyType==11){
                                return "天安车险";
                            }else if(d.companyType==12){
                                return "安邦车险";
                            }else if(d.companyType==13){
                                return "华泰车险";
                            }else if(d.companyType==14){
                                return "安盛天平车险";
                            }else if(d.companyType==15){
                                return "中银车险";
                            }else if(d.companyType==16){
                                return "永诚车险";
                            }
                        }
                    }
                    ,{field:'code', title: '保单号',  width:'8%'}
                    ,{field:'applicant', title: '投保人',  width:'6%'}
                    ,{field:'phone', title: '手机号码',  width:'8%'}
                    ,{field:'plateNumber', title: '车牌号',  width:'8%'}
                    ,{field:'frameNumber', title: '车架号',  width:'8%'}
                    ,{field:'carInfo', title: '汽车品牌',  width:'10%'}
                    ,{field:'policyMoney', title: '投保金额',  width:'6%'}
                    ,{field:'returnMoney', title: '礼包金额',  width:'6%'}
                    ,{field:'expiryTime', title: '参保日期',  width:'6%',templet:
                        function(d){
                            return $tool.formatDate(d.expiryTime);
                        }
                    }
                    , {field: 'status', title: '保单状态', width: '7%', templet:
                        function (d) {
                            if (d.status == 1) {
                                return "正常"
                            } else if (d.status == 0) {
                                return "即将过期"
                            } else if (d.status == -1) {
                                return "已过期"
                            }
                        }

                    }
                    ,{title: '操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器


                ]]
                ,id: 'testReload'
                ,page: true
//	        ,height: 600
            });
		}




	    
	    
	    form.on("submit(queryPolicy)", function (data) {
	        var code = data.field.code;
	        var phone = data.field.phone;
            var merchantId = data.field.merchantId;
            var status = data.field.status;

            var minPolicyMoney=data.field.minPolicyMoney;
            var maxPolicyMoney=data.field.maxPolicyMoney;
            if((minPolicyMoney!='' &&maxPolicyMoney=='') || (minPolicyMoney=='' &&maxPolicyMoney!='')  ){
                layer.msg('请填写正确的保单金额范围');
                return;
            }else{
                var reg = new RegExp("^[0-9]*$");
                var flag= reg.test(minPolicyMoney);
                var flag2= reg.test(maxPolicyMoney);
                if(!(flag && flag2)){
                    layer.msg('请填写正确的保单金额范围');
                    return;
                }
            }
	        //表格重新加载
	        tableIns.reload({
	            where:{
	            	code:code,
	            	phone:phone,
                    merchantId:merchantId,
                    status:status,
                    minPolicyMoney:minPolicyMoney,
                    maxPolicyMoney:maxPolicyMoney

	            }
	        });

	        return false;
	    });
	    
	    table.on('tool(policys)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    	  var data = obj.data; //获得当前行数据
	    	  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	    	  var tr = obj.tr; //获得当前行 tr 的DOM对象
	    	 
	    	  if(layEvent === 'detail'){ //查看
	    	    //do somehing
	    	  } else if(layEvent === 'del'){ //删除
	    	    layer.confirm('真的删除该保单吗', function(index){
	    	      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
	    	      layer.close(index);
	    	      //向服务端发送删除指令
	    	      var ids=obj.data.id;
	    	      $.ajax({  
	    	            type: "DELETE",
	    	            url:"/policys/delete/"+ids,
	    	            success: function(res) {
	    	            	if(res.code==2000){
	    	            		top.layer.close(index);
	    	          			top.layer.msg("删除成功");
	    	            	}
	    	            
	    	            } ,
	    		 		 error: function(res) {
	    		             alert("Connection error");  
	    		         } 
	    	        }); 
	    	    });
	    	  } else if(layEvent === 'edit'){ //编辑
	    	    //do something
	    	     var ids=obj.data.id;
	    	     editPolicy(ids);
	    	    
	    	    //同步更新缓存对应的值
	    	    obj.update({
	    	      username: '123'
	    	      ,title: 'xxx'
	    	    });
	    	  }
	    	});
	});
	
	$(window).one("resize",function(){
		$(".policysAdd_btn").click(function(){
			var index = layui.layer.open({
				title : "添加产品",
				type : 2,
				content : "policys/policysAdd.html",
				success : function(layero, index){
					
				}
			})			
			layui.layer.full(index);
		})
	}).resize();
	
 	 var categoryList;
	  $.ajax({
         type : "GET",
         url: 'categorys/getAlls',
         dataType:'json',
      	  async:false,
         success: function(data){
         	var list=data.data;
         	categoryList=list;
         	var html='';
             for(var i=0;i<list.length;i++){	
                 html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>"
             }
             $("select[name='categoryId']").append(html);
             renderForm();//表单重新渲染，要不然添加完显示不出来新的option
         }
     }); 
	
	  
	  function editPolicy(id){
	        var index = layui.layer.open({
	            title: "编辑保单",
	            type: 2,
	            content: "policys/policysEdit.html?id="+id,
	            success: function (layero, index) {

	            }
	        });

	        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	        $(window).resize(function () {
	            layui.layer.full(index);
	        });
	        layui.layer.full(index);
	  }
	</script> 
</body>
</html>